package org.daochong.spring.interceptor;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.daochong.lang.JdbcTemplate;
import org.daochong.lang.SystemLogger;
import org.daochong.lang.uai.UniteAccessInterfaceFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcTemplateInterceptor implements HandlerInterceptor {

	private UniteAccessInterfaceFactory uaiFactory;
	private boolean debug;

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public UniteAccessInterfaceFactory getUaiFactory() {
		return uaiFactory;
	}

	public void setUaiFactory(UniteAccessInterfaceFactory uaiFactory) {
		this.uaiFactory = uaiFactory;
	}

	@SuppressWarnings("resource")
	private void debug(String prefix, String uri) {
		for (JdbcTemplate jdbc : this.getUaiFactory().getThreadLocaleObjects(JdbcTemplate.class)) {
			DataSource ds = jdbc.getDataSource();
			if (ds instanceof ComboPooledDataSource) {
				ComboPooledDataSource cds = (ComboPooledDataSource) ds;
				try {

					SystemLogger.info(prefix, "[Thread:" + Thread.currentThread().getId() + "] [" + uri + "] Jdbc "
							+ prefix + " 总数:" + cds.getNumConnectionsAllUsers());
					SystemLogger.info(prefix, "[Thread:" + Thread.currentThread().getId() + "] [" + uri + "] Jdbc "
							+ prefix + " 占用:" + cds.getNumBusyConnectionsAllUsers());
					SystemLogger.info(prefix, "[Thread:" + Thread.currentThread().getId() + "] [" + uri + "] Jdbc "
							+ prefix + " 空闲:" + cds.getNumIdleConnectionsAllUsers());
				} catch (SQLException e) {
					e.printStackTrace();
				}
			} else if (ds instanceof DruidDataSource) {
				DruidDataSource dds = (DruidDataSource) ds;
				SystemLogger.info(prefix, "[Thread:" + Thread.currentThread().getId() + "] [" + uri + "] Jdbc " + prefix
						+ " 总数:" + dds.getPoolingCount());
				SystemLogger.info(prefix, "[Thread:" + Thread.currentThread().getId() + "] [" + uri + "] Jdbc " + prefix
						+ " 占用:" + dds.getActiveCount());
				SystemLogger.info(prefix, "[Thread:" + Thread.currentThread().getId() + "] [" + uri + "] Jdbc " + prefix
						+ " 空闲:" + (dds.getPoolingCount() - dds.getActiveCount()));
			}
		}
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if (this.isDebug()) {
			debug("BEFORE", request.getRequestURI());
		}
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		for (JdbcTemplate jdbc : this.getUaiFactory().getThreadLocaleObjects(JdbcTemplate.class)) {
			if (jdbc.isTran()) {
				try {					if (ex == null) {
						jdbc.commit();
					} else {
						jdbc.rollback();
					}
				} finally {
					jdbc.endTran();
				}
			}
		}
		if (this.isDebug()) {
			debug("AFTER", request.getRequestURI());
		}
	}
}
